package com.example.test.controller;

import com.example.test.Bean.UserBean;
import com.example.test.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 控制层 controller
 */
@Controller
public class UserController {
    //自动装配 bean
    @Autowired
    UserService userservice;

//    @RequestMapping("/users")
//    public String showUser(ModelMap modelmap){
//        List<UserBean> allUser = userservice.getAllUser();
//        modelmap.put("userlist",allUser);
//        return "users";
//    }
     //添加用户
    @RequestMapping("/addUser")
    @ResponseBody
    public Map addUser(UserBean userBean){
        Map<String,Object> map = new HashMap<String,Object>();
            int flag = userservice.insertUser(userBean);
            if(flag == 1){
                map.put("msg","增加用户成功！");
                return map;
            }else{
                map.put("msg","增加用户失败！");
                return map;
            }
    }
    //id 删除用户
    @RequestMapping(value = "/deleteUser+{id}")
    public String deletUser(@PathVariable("id") String id,ModelMap modelMap){
        int i = userservice.deleteUser(id);
        List<UserBean> allUser = userservice.getAllUser();
        modelMap.addAttribute("userlist",allUser);
        if(i==1){
            return "users";
        }else{
            return "error";
        }
    }
    //id 查询用户
    @RequestMapping(value = "/user+{id}")
    public String getUserByid(@PathVariable("id") String id,ModelMap modelmap){
        UserBean user = userservice.getUserById(id);
        modelmap.addAttribute("user",user);
        return "userInfo";
    }
    //修改
    @RequestMapping(value = "/modifyUser")
    @ResponseBody
    public Map updateUaer(UserBean user){
        Map<String,Object> map = new HashMap<String,Object>();
        int i = userservice.updateUser(user);
        if(i==1){
            map.put("msg","修改信息成功");
            return map;
        }else {
            map.put("msg","修改信息失败");
            return map;
        }
    }
    //分页
    @RequestMapping("/users")
    public String index(Model model, @RequestParam(defaultValue = "2") Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize){
        PageInfo<UserBean> pageInfo = userservice.selectUser4Page(pageNum,pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("userlist",pageInfo.getList());
        return "users";
    }
    //退出登入
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "index";
    }
}
